package com.example.taobaounion.presenter.impl;

import com.example.taobaounion.model.Api;
import com.example.taobaounion.model.bean.TickParams;
import com.example.taobaounion.model.bean.TicketResult;
import com.example.taobaounion.presenter.ITicketPresenter;
import com.example.taobaounion.utils.LogUtils;
import com.example.taobaounion.utils.RetrofitManager;
import com.example.taobaounion.utils.UrlUtils;
import com.example.taobaounion.view.ITicketPagerCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterImpl implements ITicketPresenter {
    private ITicketPagerCallback mViewCallback=null;
    private String mCover =null;
    private TicketResult mTicketResult;

    enum  LoadState{
        LOADING,SUCCESS,ERROR,NONE
    }
    private LoadState mCurrentState =LoadState.NONE;
    @Override
    public void getTicket(String title, String url, String cover) {

        onTicketLoading();
        this.mCover =cover;
        LogUtils.d(this,"title--->"+title);
        LogUtils.d(this,"url--->"+url);
        LogUtils.d(this,"cover--->"+cover);
        String targetTicketUrl = UrlUtils.getTicketUrl(url);
        //去获取淘口令
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        TickParams tickParams = new TickParams(targetTicketUrl, title);
        Call<TicketResult> task=api.getTicket(tickParams);
        task.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                LogUtils.d(TicketPresenterImpl.this,"result code--->"+code);
                if(code== HttpURLConnection.HTTP_OK){
                    //请求成功
                    mTicketResult = response.body();
                    LogUtils.d(TicketPresenterImpl.this,"result--->"+ mTicketResult);
                    //通知UI更新
                    onTicketLoadedSuccess();
                }else {
                    //请求失败
                    onLoadedTicketError();
                }
            }
            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                //失败
                onLoadedTicketError();

            }
        });
    }

    private void onTicketLoadedSuccess() {
        if (mViewCallback != null) {
            mViewCallback.onTicketLoaded(mCover, mTicketResult);
        }else {
            mCurrentState =LoadState.SUCCESS;
        }
    }

    private void onLoadedTicketError() {
        if (mViewCallback != null) {
            mViewCallback.onError();
        }else {
            mCurrentState =LoadState.ERROR;
        }
    }

    /**
     * 因为就一个页面需要用到，如果多个页面需要用到可以用集合保存回调
     * @param callback
     */
    //通过设置枚举的不同状态，可以解决当数据传回来过快，还没注册mViewCallback为null的问题，如果为null再次执行一次
    @Override
    public void registerViewCallback(ITicketPagerCallback callback) {
        this.mViewCallback=callback;
        if (mCurrentState!=LoadState.NONE){
            //说明状态已经改变了
            //更新UI
            if(mCurrentState==LoadState.SUCCESS){
                onTicketLoadedSuccess();
            }else if(mCurrentState==LoadState.ERROR){
                onLoadedTicketError();
            }else if(mCurrentState==LoadState.LOADING){
                onTicketLoading();
            }
        }
    }

    private void onTicketLoading() {
        if (mViewCallback != null) {
            mViewCallback.onLoading();
        }else {
            mCurrentState =LoadState.LOADING;
        }
    }

    @Override
    public void unregisterViewCallback(ITicketPagerCallback callback) {
           this.mViewCallback=null;
    }
}
